package com.diploma.interaction.controller;

import com.diploma.interaction.model.Detail;
import com.diploma.interaction.model.DjnsrxxTest;
import com.diploma.interaction.model.DjnsrxxkzTest;
import com.diploma.interaction.model.User;
import com.diploma.interaction.service.DjNsrxxService;
import com.diploma.interaction.service.DjNsrxxkzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.alibaba.fastjson.JSON;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/v1/djNsrxx")
@Slf4j
public class DjNsrxxController {

    @Autowired
    DjNsrxxService djNsrxxService;

    @Autowired
    DjNsrxxkzService djNsrxxkzService;

    @ApiOperation(value = "增加数据")
    @ApiImplicitParams(@ApiImplicitParam(paramType="body",dataType = "DjNsrxx",name="djNsrxx",value="插入的值"))
    @PostMapping(value = "insert",produces = "application/json")
    public void insert(@RequestBody DjnsrxxTest djNsrxx){
        log.info("日志要这么写,diNsrxx={}", JSON.toJSONString(djNsrxx));
        djNsrxxService.insertSelective(djNsrxx);
    }

    @PostMapping(value = "select",produces = "application/json")
    public DjnsrxxTest select(@RequestBody DjnsrxxTest djNsrxx){
        log.info("日志要这么写,diNsrxx={}", JSON.toJSONString(djNsrxx));
        return djNsrxxService.selectByPrimaryKey(djNsrxx.getDjxh().intValue());

    }

    @PostMapping(value = "getDetail",produces = "application/json")
    public Collection<Detail> getDetail(@RequestBody User user){
        log.info("user={}", JSON.toJSONString(user));
        //return djNsrxxService.selectByPrimaryKey(djNsrxx.getDjxh().intValue());
        List<DjnsrxxTest> faren=djNsrxxService.selectByZjlxAndZjhm(user.getSfzjzlDm(),user.getSfzjhm());
        Map<BigDecimal, Detail> map = new HashMap<BigDecimal,Detail>();
        for (int i = 0; i < faren.size(); i++) {
            DjnsrxxTest tmp=faren.get(i);
            map.put(tmp.getDjxh(),Detail.builder()
                    .djxh(tmp.getDjxh())
                    .nsrsbh((String) tmp.getNsrsbh())
                    .nsrmc((String) tmp.getNsrmc())
                    .isFaren(true).build());
        }
        List<DjnsrxxkzTest> kz=djNsrxxkzService.selectByZjlxAndZjhm(user.getSfzjzlDm(),user.getSfzjhm());
        for (int i = 0; i < kz.size(); i++) {
            DjnsrxxkzTest tmp=kz.get(i);
            Detail de=map.get(tmp.getDjxh());
            if(de==null){
                DjnsrxxTest nsrxx=djNsrxxService.selectByDjxh(tmp.getDjxh());
                map.put(tmp.getDjxh(),Detail.builder()
                        .djxh(tmp.getDjxh())
                        .nsrsbh((String) nsrxx.getNsrsbh())
                        .nsrmc((String) nsrxx.getNsrmc())
                        .isFaren(true).build());
            }else{
                //caiwu
                if(tmp.getCwfzrsfzjzlDm().equals(user.getSfzjzlDm()) && tmp.getCwfzrsfzjhm().equals(user.getSfzjhm())){
                    de.setCaiwu(true);
                }else if(tmp.getBsrsfzjzlDm().equals(user.getSfzjzlDm())&& tmp.getBsrsfzjhm().equals(user.getSfzjhm())){
                    de.setBanshui(true);
                }
                map.put(tmp.getDjxh(),de);
                //banshui
            }
        }
        // 获得集合
        return map.values();
    }

    @PostMapping(value = "getSsgly",produces = "application/json")
    public DjnsrxxTest getSsgly(@RequestBody DjnsrxxTest djNsrxx){
        log.info("日志要这么写,diNsrxx={}", JSON.toJSONString(djNsrxx));
        return djNsrxxService.selectByNsrsbh(djNsrxx.getNsrsbh().toString());

    }
}
